package com.block.storage.service.oss;

import cn.hutool.core.collection.CollUtil;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.CopyObjectResult;
import com.aliyun.oss.model.GenericRequest;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.aliyuncs.exceptions.ClientException;
import com.block.storage.config.CosTenConfig;
import com.block.storage.config.OssAliConfig;
import com.block.storage.model.OssAuthInfoParam;
import com.block.storage.model.dto.OSSAuthInfoDTO;
import com.block.storage.service.ICosService;
import com.block.storage.service.IOssService;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sts.v20180813.StsClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.net.URL;
import java.util.Date;
import java.util.List;

@Service
public class CssTenService implements ICosService {


    @Autowired
    CosTenConfig cosTenConfig;


    @Override
    public OSSAuthInfoDTO getAuthParam(OssAuthInfoParam ossAuthInfoParam) throws TencentCloudSDKException {


        //发起腾讯云客户端请求
        Credential cred = new Credential(cosTenConfig.getAccessKey(), cosTenConfig.getAccessSecret());

        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("sts.tencentcloudapi.com");

        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);


        Credential credentials = new StsClient(cred, cosTenConfig.getRegionId(), clientProfile).getCredential();

        OSSAuthInfoDTO ossAuthInfoDTO = new OSSAuthInfoDTO();
        ossAuthInfoDTO.setAccessKeyId(credentials.getSecretId());
        ossAuthInfoDTO.setAccessKeySecret(credentials.getSecretKey());
        ossAuthInfoDTO.setSecurityToken(credentials.getToken());
        //ossAuthInfoDTO.setExpiration(response.getExpiration());
        ossAuthInfoDTO.setBucketName(cosTenConfig.getBucketName());
        ossAuthInfoDTO.setEndpoint(cosTenConfig.getEndpoint());
        return ossAuthInfoDTO;
    }


}
